<div class="layui-fluid layui-layout-admin">
	<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
		<legend class="title"></legend>
	</fieldset>

	<form class="layui-form" action="">
		<div class="layui-form-item">
			<label class="layui-form-label">名称</label>
			<div class="layui-input-inline">
				<input type="text" name="title" autocomplete="off" placeholder="请输入优惠券名称" class="layui-input"
					value="">
			</div>
		</div>

		<div class="layui-form-item">
			<label class="layui-form-label">优惠金额</label>
			<div class="layui-input-inline">
				<input type="text" name="money" autocomplete="off" placeholder="请输入优惠金额" class="layui-input"
					value="">
			</div>
		</div>

		<div class="layui-form-item">
			<label class="layui-form-label">使用限制</label>
			<div class="layui-input-inline">
				<input type="text" name="restrict" autocomplete="off" placeholder="请输入使用限制" class="layui-input"
					   value="">
			</div>
			<div class="layui-form-mid layui-word-aux">满多少钱可用，0为不限制</div>
		</div>

		<div class="layui-form-item">
			<label class="layui-form-label">发放数量</label>
			<div class="layui-input-inline">
				<input type="text" name="num" autocomplete="off" placeholder="请输入发放数量" class="layui-input"
					   value="">
			</div>
			<div class="layui-form-mid layui-word-aux">0为不限制数量</div>
		</div>

		<div class="layui-form-item">
			<label class="layui-form-label">永久有效</label>
			<div class="layui-input-block">
				<input type="checkbox" class="is_permanent_validity" lay-filter="is_permanent_validity" lay-skin="switch" lay-text="是|否">
			</div>
		</div>

		<div class="layui-form validity">
			<div class="layui-form-item">
				<div class="layui-inline">
					<label class="layui-form-label">开始时间</label>
					<div class="layui-input-inline">
						<input type="text" class="layui-input" id="start_time" autocomplete="off" name="start_time" placeholder="yyyy-MM-dd HH:mm:ss">
					</div>
				</div>
				<div class="layui-inline">
					<label class="layui-form-label">结束时间</label>
					<div class="layui-input-inline">
						<input type="text" class="layui-input" id="end_time" autocomplete="off" name="end_time" placeholder="yyyy-MM-dd HH:mm:ss">
					</div>
				</div>
			</div>
		</div>

		<div class="layui-form-item">
			<div class="layui-input-block">
				<div class="layui-footer">
					<input type="hidden" name="id" value="0" />
					<input type="hidden" name="is_permanent_validity" value="0" />
					<input type="hidden" name="list_table_name" value="coupon-list" />
					<button class="layui-btn" lay-submit lay-filter="LAY-send-submit" id="LAY-send-submit">立即提交</button>
				</div>
			</div>
		</div>
	</form>
</div>

<script type="text/html" template lay-done="layui.data.sendParams(d.params)">
</script>

<script >
	layui.data.sendParams = function(params){
		console.log(params);
		layui.use(['form', 'laydate'], function() {
			let $ = layui.jquery,
				laydate = layui.laydate,
				form = layui.form,
				router = layui.router();

			//开始时间
			laydate.render({
				elem: '#start_time'
				,type: 'datetime'
			});
			//结束时间
			laydate.render({
				elem: '#end_time'
				,type: 'datetime'
			});

			form.render();

			form.on('switch(is_permanent_validity)', function(data){
				if (data.elem.checked) {
					$(".validity").hide();
					$("input[name='is_permanent_validity']").val(1);
				} else {
					$(".validity").show();
					$("input[name='is_permanent_validity']").val(0);
				}
			});

			let _index = layer.load();
			$(".layui-form").attr('action', layui.setter.API_HOST + 'admin/coupon/save')
			let _coupon_id = params.id | 0;
			$(".title").text((_coupon_id > 0 ? '编辑' : '添加') + '优惠券');
			$("input[name='is_permanent_validity']").val(0);
			layui.admin.req({
				url: layui.setter.API_HOST + 'admin/coupon/save',
				data: {
					id: _coupon_id
				},
				done: function(res) {
					console.log(res);
					layer.close(_index);
					if (res.code == 0) {
						if (res.data) {
							$("input[name='title']").val(res.data.title);
							$("input[name='money']").val(res.data.money);
							$("input[name='restrict']").val(res.data.restrict);
							$("input[name='num']").val(res.data.num);
							$("input[name='is_permanent_validity']").val(res.data.is_permanent_validity);
							$("input[name='start_time']").val(res.data.start_time);
							$("input[name='end_time']").val(res.data.end_time);
							$("input[name='id']").val(res.data.id);
							if (res.data.is_permanent_validity == 1) {
								$(".is_permanent_validity").prop('checked', true);
								form.render();
							}
						}
					} else {
						layer.msg('请求失败', {icon: 5})
					}
				}
			});
		});
	}
</script>
